package com.haha.onebot.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.annotation.Resource;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.info.BuildProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.net.InetAddress;
import java.text.SimpleDateFormat;
import java.util.Date;

@Component
@Log4j2
public class AppListener implements CommandLineRunner, DisposableBean {

    @Resource
    private ApplicationContext context;

    @Resource
    private ObjectMapper objectMapper;

    @Override
    public void run(String... args) throws Exception {
        log.info("服务启动中");
        BuildProperties bean = context.getBean(BuildProperties.class);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        String hostAddress = InetAddress.getLocalHost().getHostAddress();

        String info = """
                
                
                \t\t\t==========================
                \t\t\t\t🤖应用信息
                \t\t\t\t\t应用版本: {}
                \t\t\t\t\t应用名称: {}
                \t\t\t\t\t应用编译时间: {}
                \t\t\t\t\tjava编译版本: {}
                \t\t\t\t\t应用描述: {}
                \t\t\t\t\t服务器ip: {}
                \t\t\t==========================
                
                
                """;
        log.info(info,
                bean.getVersion(),
                bean.getName(),
                sdf.format(new Date(bean.getTime().toEpochMilli())),
                bean.get("java.version"),
                bean.get("description"),
                hostAddress);

        log.info("服务启动成功，开始初始化数据");



        log.info("初始化数据完成");

    }

    @Override
    public void destroy() throws Exception {
        log.info("服务关闭，开始保存数据");
        GlobalConfig.shutDown.set(true);
        log.info("保存数据完成");
    }
}
